package com.ckf.es.controller;

import com.ckf.es.dao.EmployeeRepository;
import com.ckf.es.entity.Employee;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: serene
 * @date: 2020/9/7 11:03
 */
@RestController
@RequestMapping("es")
public class EmployeeController {

    @Autowired
    private EmployeeRepository employeeRepository;


    /**
     * 条件查询
     * @return
     */
    @GetMapping("query")
    public Employee query() {
        Employee accountInfo = employeeRepository.queryEmployeeById("1");
        System.err.println(new Gson().toJson(accountInfo));
        return accountInfo;
    }


    /**
     * 查询条数
     * @return
     */
    @GetMapping("listCount")
    public long queryAll() {
        employeeRepository.count();
        System.out.println(employeeRepository.count());
        return employeeRepository.count();
    }


    /**
     * 添加
     * @return
     */
    @PostMapping("add")
    public String add(){
        Employee employee = new Employee();
        employee.setId("1");
        employee.setFirstName("xuxu");
        employee.setLastName("zh");
        employee.setAge(26);
        employee.setAbout("i am in peking");
        employeeRepository.save(employee);
        System.err.println("add a obj");
        return "success";
    }


    /**
     * 删除
      * @return
     */
    @DeleteMapping("delete")
    public String delete(){
        Employee employee = employeeRepository.queryEmployeeById("1");
        employeeRepository.delete(employee);
        return "success";
    }

}
